﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLHD.BLL;
using QLHD.DAL;

namespace QLHD
{
    public partial class frmSay : Form
    {
        string maauto;
        public frmSay() { }
        public frmSay(frmMain form)
        {
            InitializeComponent();
            MdiParent = form;
        }

        private void frmSay_Load(object sender, EventArgs e)
        {
            dgvKhoSay.DataSource = Kho_say_DB.SelectKhoSay();
            LoadCombobox();
            LoadComboboxManhom();
            ReadOnlyTextbox();
            DisplayData(0);
            if (dgvKhoSay.RowCount <2)
            {
                cbbMaLo.Text = "";
                cbbMaNhom.Text = "";
                cbbMaTP.Text = "";
                cbbMaNX.Text = "";
            }
        }
        void LoadCombobox()
        {
            cbbMaLo.DataSource = Lo_DB.SelectLo();
            cbbMaLo.DisplayMember = "diengiai";
            cbbMaLo.ValueMember = "malo";

            cbbMaTP.DataSource = MaTP_DB.SelectMaTP_Say();
            cbbMaTP.DisplayMember = "matp";
            cbbMaTP.ValueMember = "matp";

            cbbMaNX.DataSource = MaNX_DB.SelectAllMaNX();
            cbbMaNX.DisplayMember = "manx";
            cbbMaNX.ValueMember = "manx";
        }
        void LoadComboboxManhom()
        {
            cbbMaNhom.DataSource = Nhom_DB.spSelectAllNhomByMalo(cbbMaLo.SelectedValue.ToString());
            cbbMaNhom.DisplayMember = "diengiai";
            cbbMaNhom.ValueMember = "manhom";
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbbMaLo_SelectionChangeCommitted(object sender, EventArgs e)
        {
            LoadComboboxManhom();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "Tạo mới")
            {
                btnThem.Text = "Lưu";
                dtpNgayThang.Text = DateTime.Now.ToString();                
                AutoMaKhoSay();
                txtMaKhoSay.Text = maauto;
                PrepareCombobox();
                NotReadOnlyTextbox();
                ClearTextbox();

            }
            else
            {
                try
                {
                    InsertKhoSay();
                    btnThem.Text = "Tạo mới";
                    frmSay_Load(sender, e);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtDienGiai.Focus();
                }
            }         
        }

        private void InsertKhoSay()
        {
            KhoSay item = new KhoSay();
            item.Makhosay = txtMaKhoSay.Text;
            item.NgayThang = dtpNgayThang.Text;
            item.Malo = cbbMaLo.SelectedValue.ToString();
            item.Manx = cbbMaNX.SelectedValue.ToString();
            item.Manhom = cbbMaNhom.SelectedValue.ToString();
            item.Matp = cbbMaTP.SelectedValue.ToString();
            item.Diengiai = txtDienGiai.Text;
            try
            {
                item.KLnhap_say = double.Parse(txtNhapSay.Text);
                item.KLvao_lo = double.Parse(txtVaoLo.Text);
                item.KLthu_hoi = double.Parse(txtThuHoi.Text);
                item.Xuat_kho = double.Parse(txtXuatKho.Text);
            }
            catch (Exception)
            {
                item.KLnhap_say = 0;
                item.KLvao_lo = 0;
                item.KLthu_hoi = 0;
                item.Xuat_kho = 0;                
            }
            

            Kho_say_DB.InsertKhoSay(item);
        }

        private void ClearTextbox()
        {
            txtDienGiai.Clear();
            txtNhapSay.Clear();
            txtVaoLo.Clear();
            txtThuHoi.Clear();
            txtXuatKho.Clear();
        }

        private void NotReadOnlyTextbox()
        {
            txtDienGiai.ReadOnly = false;
            txtNhapSay.ReadOnly = false;
            txtVaoLo.ReadOnly = false;
            txtThuHoi.ReadOnly = false;
            txtXuatKho.ReadOnly = false;            
        }
        private void ReadOnlyTextbox()
        {
            txtDienGiai.ReadOnly = true;
            txtNhapSay.ReadOnly = true;
            txtVaoLo.ReadOnly = true;
            txtThuHoi.ReadOnly = true;
            txtXuatKho.ReadOnly = true;    
        }

        private void AutoMaKhoSay()
        {
            maauto = "";
            SqlConnection con = Connection.GetConnection();
            con.Open();
            string select = "select top 1 makhosay from tblQLKho_Say where NgayThang = '" + dtpNgayThang.Text + "' order by makhosay desc";
            DataTable dt = new DataTable();
            SqlCommand command = new SqlCommand(select, con);
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dt);

            if (dt.Rows.Count == 0)
            {
                maauto = dtpNgayThang.Text.Replace("/", "") + "1";
            }
            else
            {
                maauto = (Convert.ToInt32(dt.Rows[0][0]) + 1).ToString();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                Kho_say_DB.DeleteKhoSay(txtMaKhoSay.Text);
                frmSay_Load(sender, e);
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private void dgvKhoSay_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DisplayData(e.RowIndex);
        }
        private void DisplayData(int index)
        {
            try
            {
                txtMaKhoSay.Text = dgvKhoSay.Rows[index].Cells[0].Value.ToString();
                dtpNgayThang.Text = dgvKhoSay.Rows[index].Cells[1].Value.ToString();
                cbbMaLo.Text = dgvKhoSay.Rows[index].Cells[2].Value.ToString();
                cbbMaNhom.Text = dgvKhoSay.Rows[index].Cells[3].Value.ToString();
                cbbMaNX.Text = dgvKhoSay.Rows[index].Cells[4].Value.ToString();
                cbbMaTP.Text = dgvKhoSay.Rows[index].Cells[5].Value.ToString();
                txtDienGiai.Text = dgvKhoSay.Rows[index].Cells[6].Value.ToString();
                txtNhapSay.Text = dgvKhoSay.Rows[index].Cells[7].Value.ToString();
                txtVaoLo.Text = dgvKhoSay.Rows[index].Cells[8].Value.ToString();
                txtThuHoi.Text = dgvKhoSay.Rows[index].Cells[9].Value.ToString();
                txtXuatKho.Text = dgvKhoSay.Rows[index].Cells[10].Value.ToString();    
            }
            catch (Exception)
            {
                return;
            }
       
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            frmSay_Load(sender, e);
            btnThem.Text = "Tạo mới";
            btnSua.Text = "Cập nhật";
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text == "Cập nhật")
            {
                btnSua.Text = "Lưu";                                
                NotReadOnlyTextbox();
                ClearTextbox();           
            }
            else
            {
                try
                {
                    KhoSay item = new KhoSay();
                    item.Makhosay = txtMaKhoSay.Text;
                    item.NgayThang = dtpNgayThang.Text;
                    item.Malo = cbbMaLo.SelectedValue.ToString();
                    item.Manx = cbbMaNX.SelectedValue.ToString();
                    item.Manhom = cbbMaNhom.SelectedValue.ToString();
                    item.Matp = cbbMaTP.SelectedValue.ToString();
                    item.Diengiai = txtDienGiai.Text;
                    item.KLnhap_say =double.Parse( txtNhapSay.Text);
                    item.KLvao_lo =double.Parse( txtVaoLo.Text);
                    item.KLthu_hoi =double.Parse( txtThuHoi.Text);
                    item.Xuat_kho = double.Parse(txtXuatKho.Text);

                    Kho_say_DB.UpdateKhoSay(txtMaKhoSay.Text, item);
                    btnSua.Text = "Cập nhật";
                    frmSay_Load(sender, e);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtDienGiai.Focus();
                }
            }         
        }
        private void PrepareCombobox()
        {
            cbbMaLo.Text = "--Chọn mã lô--";
            cbbMaNhom.Text = "--Chọn mã nhóm--";
            cbbMaTP.Text = "--Chọn mã loại--";
            cbbMaNX.Text = "--Chọn mã N/X--";
                
        }
    }
}
